package com.qdaily.ui.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.*;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.supportlibrary.util.QLog;
import com.qdaily.ui.QDBaseActivity;
import com.qdaily.ui.R;
import com.qdaily.ui.data.Constants;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.BaseEntity;
import com.qdaily.ui.ui.views.TitleBar;
import com.qdaily.ui.util.QDUtil;

/**
 * 注册界面。
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/13/15
 * Time: 17:29
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class RegisterActivity extends QDBaseActivity implements View.OnClickListener {
    private android.widget.EditText editTextRegisterEmail;
    private android.widget.EditText editTextRegisterPassword;
    private android.widget.ImageButton imageViewRegisterPwdVisiable;
    private android.widget.TextView textViewRegisterDisclaimer;
    private android.widget.TextView textViewRegisterNextStep;
    private LinearLayout linearLayoutRegisterPwd;
    private TitleBar viewTitleHeaderBar;
    //是否显示密码
    private boolean mPwdDisplayFlg = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViewRes();
    }

    private void initViewRes() {
        this.linearLayoutRegisterPwd = (LinearLayout) findViewById(R.id.linearLayoutRegisterPwd);
        this.textViewRegisterNextStep = (TextView) findViewById(R.id.textViewRegisterNextStep);
        this.textViewRegisterDisclaimer = (TextView) findViewById(R.id.textViewRegisterDisclaimer);
        this.imageViewRegisterPwdVisiable = (ImageButton) findViewById(R.id.imageViewRegisterPwdVisiable);
        this.editTextRegisterPassword = (EditText) findViewById(R.id.editTextRegisterPassword);
        this.editTextRegisterEmail = (EditText) findViewById(R.id.editTextRegisterEmail);
        this.textViewRegisterNextStep.setOnClickListener(this);
        this.imageViewRegisterPwdVisiable.setOnClickListener(this);
        this.textViewRegisterDisclaimer.setOnClickListener(this);
        this.viewTitleHeaderBar = (TitleBar) findViewById(R.id.viewTitleHeaderBar);
        this.viewTitleHeaderBar.setTitle(R.string.register);
        this.viewTitleHeaderBar.onBack();

        this.editTextRegisterEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editTextRegisterEmail.getText().toString().length() > 0) {
                    editTextRegisterEmail.setBackgroundResource(R.drawable.golden_border_background);
                } else {
                    editTextRegisterEmail.setBackgroundResource(R.drawable.gray_border_background);
                }
            }
        });

        this.editTextRegisterPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (editTextRegisterPassword.getText().toString().length() > 0) {
                    linearLayoutRegisterPwd.setBackgroundResource(R.drawable.golden_border_background);
                } else {
                    linearLayoutRegisterPwd.setBackgroundResource(R.drawable.gray_border_background);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.textViewRegisterNextStep:
                nextStep();
                break;
            case R.id.imageViewRegisterPwdVisiable:
                isPasswordVisiable();
                break;
            case R.id.textViewRegisterDisclaimer:
                displayAgreement();
                break;
        }
    }

    private void displayAgreement() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra(WebViewActivity.EXTRA_WEBVIEW_URL, Constants.AGREEMENT);
        startActivity(intent);
    }

    private void isPasswordVisiable() {
        if (!mPwdDisplayFlg) {
            editTextRegisterPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            editTextRegisterPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        mPwdDisplayFlg = !mPwdDisplayFlg;
        editTextRegisterPassword.postInvalidate();
        CharSequence text = editTextRegisterPassword.getText();
        Spannable spanText = (Spannable) text;
        Selection.setSelection(spanText, text.length());
        imageViewRegisterPwdVisiable.setSelected(mPwdDisplayFlg);
    }

    private String email = "";
    private String password = "";

    private void nextStep() {
        email = editTextRegisterEmail.getText().toString();
        if (TextUtils.isEmpty(email)) {
            displayToast(R.string.please_entry_email);
            return;
        }
        if (!QDUtil.isEmail(email)) {
            displayToast(R.string.entry_email_is_wrong);
            return;
        }
        password = editTextRegisterPassword.getText().toString();
        if (TextUtils.isEmpty(password)) {
            displayToast(R.string.please_entry_password);
            return;
        }
        if (password.length() < 8) {
            displayToast(R.string.entry_password_is_wrong);
            return;
        }

        getRequestManager().requestRegisterFirstStep(email, password);
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);

        if (params instanceof NetConfigs.RequestRegisterFirstStep) {
            QLog.json(response.toString());
            BaseEntity baseEntity = QDJsonUtil.Json2Object(response.toString(), BaseEntity.class);
            if (baseEntity.getMeta().getStatus() == NetConfigs.HTTP_RESPONSE_200) {
                Intent intent = new Intent(this, RegisterNextStepActivity.class);
                intent.putExtra(RegisterNextStepActivity.KEY_REGISTER_EMAIL, email);
                intent.putExtra(RegisterNextStepActivity.KEY_REGISTER_PASSWORD, password);
                startActivity(intent);
            } else {
                displayToast(baseEntity.getMeta().getMsg());
            }
        }
    }

}
